Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-mock

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-mock

Promise mock library for synchronous Promise testing

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
973
increased by81.53%
Maintainers
1
Weekly downloads
 
Created
Source

promise-mock Build Status

Promise mocking library to make Promises resolve synchronously

Why

  • Tests are faster
  • Don't need done() function
  • Stack traces are preserved / Easier debugging
  • Can be easier to write tests (depends)

Don't Promises need to be async or else my code will break?

Nope

Use with Node and Webpack/Browserify

npm install promise-mock

Simple use with Mocha or Jasmine

Get the result of a Promise PromiseMock.getResult

var PromiseMock = require('promise-mock');

describe('testing', function() {
    beforeEach(function() {
        PromiseMock.install()
    });
    afterEach(function() {
        PromiseMock.uninstall();
    });
    it('Sync Promise test', function() {
        var result = PromiseMock.getResult(Promise.resolve('hello')));
        expect(result).toBe('hello');
    });
    
});

Handle errors synchronously

var PromiseMock = require('promise-mock');

describe('testing', function() {
    beforeEach(function() {
        PromiseMock.install()
    });
    afterEach(function() {
        PromiseMock.uninstall();
    });
    it('Sync Promise test', function() {
        expect(function() {
            PromiseMock.getResult(Promise.reject(new Error('An error')));
        }).toThrow(new Error('An Error'));
    });
});

Execute a single async callback

var PromiseMock = require('promise-mock');

describe('testing', function() {
    beforeEach(function() {
        PromiseMock.install()
    });
    afterEach(function() {
        PromiseMock.uninstall();
    });
    it('Sync Promise test', function() {
        var result;
        Promise.resolve('hello').then(function(data) {
            result = data;
        });
        Promise.run();
        expect(result).toBe('hello');
    });
    
});

Resolve all pending Promises

var PromiseMock = require('promise-mock');

describe('testing', function() {
    beforeEach(function() {
        PromiseMock.install()
    });
    afterEach(function() {
        PromiseMock.uninstall();
    });
    it('Sync Promise test', function() {
        var result1, result2, result3;
        Promise.resolve('hello1').then(function(data) {
            result1 = data;
            return 'hello2';
        }).then(function(data) {
            result2 = data;
            return 'hello3';
        }).then(function(data) {
            result3 = data;
        });;
        Promise.runAll();
        expect(result1).toBe('hello1');
        expect(result2).toBe('hello2');
        expect(result3).toBe('hello3');
    });
    
});

Testing

npm install
npm test

License

MIT

Keywords

FAQs

Package last updated on 27 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc